<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\UserToken;
use Illuminate\Support\Facades\DB; 
use App\Models\BaseModel;
use Qcloud\Sms\SmsSingleSender;

/** 
 * 公共
 */
class BaseController extends Controller
{
    /** 
     *  接口返回方法
     *
     */
	public function send($code,$msg,$arr=''){
		$array=[
			'code'=>(string) $code,
			'msg'=>trim($msg),
		];
		if(!empty($arr)){
			$array['data']=$this->forString($arr);
		}else{
			$array['data']=[];
		}
		//print_r($array);die;
		//var_dump($array);die;
		echo json_encode($array,JSON_UNESCAPED_UNICODE);exit;
	}
    /** 
     *  获取用户基本信息
     *  
     */
    function getUserInfo($uid){
        $arr=(new BaseModel('userinfo'))->getOne(['id'=>$uid],['rz','id','tel']);
        if($arr['rz']==0) $info['name']='';
        if($arr['rz']==1){
            $name=(new BaseModel('userinfo_personal'))->getOne(['uid'=>$arr['id']]);
            $info['name']=$name['real_name'];
            $info['tel']=$arr['tel'];
        }
        if($arr['rz']==2){
            $name=(new BaseModel('userinfo_mechanism'))->getOne(['uid'=>$arr['id']]);
            $info['name']=$name['company'];
            $info['tel']=$arr['tel'];
        }
        return $info;
    }
	/**
	 * 转字符串格式
	 *
	 */
	public function forString($arr){
		if(!is_array($arr))	return (string) $arr;
		foreach($arr as $k=>$v){
			if(is_array($v)){
				$this->forString($v);
			}else{
				$arr[$k]=(string) $v;
			}
		}
		return $arr;
	}

	/**
	 *	加密生成token
	 *	@return str $token
	 */
	public function creatToken($string,$skey ='wangxindahaoren'){
		$string_a=$string.rand(1,504377497);
		$strArr = str_split(base64_encode($string_a));
        $strCount = count($strArr);
        foreach (str_split($skey) as $key => $value)
            $key < $strCount && $strArr[$key].=$value;
        return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
	}
	/**
	 *	解密验证token 返回用户id
	 *	@return int $id
	 */
	function getIdByToken($token) {
        $arr=(new BaseModel('ispot_user_info'))->getAll(['token'=>$token]);
        if(!$arr)	$this->send(408,config('msg.msg_408'));
        return $arr[0]['id'];
    }
    /**
     *	验证openid 返回用户id
     *	@return int $id
     */
    function getIdOne($openId) {
        $arr=(new BaseModel('userinfo'))->getAll(['openId'=>$openId]);
        if(!$arr)	$this->send(409,config('msg.msg_409'));
        return $arr[0]['id'];
    }
    function getCompanyId($id) {
        $arr=(new BaseModel('userinfo'))->getAll(['id'=>$id]);
        if(!$arr)	$this->send(408,config('msg.msg_408'));
        return $arr[0]['company_id'];
    }
	/*function getId($string, $skey = 'wangxindahaoren') {
        $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
        $strCount = count($strArr);
        foreach (str_split($skey) as $key => $value)
            $key <= $strCount  && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
        $token=base64_decode(join('', $strArr));
        $arr=(new UserToken())->findAll(['token'=>$string]);
        if(!$arr)	$this->send(404,'token失效');
        return $arr[0]['u_id'];
    }*/
    /** 
     *  查看版本信息
     * 
     */
    public function version() {
		$arr= DB::select('select * from version');
		if(!$arr) $this->send(501,config('msg.msg_501'));
		$array=array_map('get_object_vars', $arr);
		$this->send(200,config('msg.msg_200'),['version'=>$array[0]['version']]);
    }


    /**
	 *	验证是否缺少参数
	 *
	 */
    public function params($array,$issetArray=''){
    	if(!$issetArray){
    		$issetArray=[];
    	}
    	//$noMust = [];
		foreach ($array as $key => $value) {
			if(!in_array($key,$issetArray)) {
				if(empty($value)) $this->send(403,config('msg.msg_403'));
			}
		}
		return true;
    }
   
    function getRandomString($len, $chars=null)  
    {  
        if (is_null($chars)) {  
            $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
        }  
        mt_srand(10000000*(double)microtime());  
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {  
            $str .= $chars[mt_rand(0, $lc)];  
        }  
        return $str;  
    }  
   
    /** 
     *  手机号正则验证
     * 
     */
    public function telVerification($tel){
        $tel=str_replace(' ', '', $tel);
        if(!preg_match("/^1[345678]\d{9}$/", $tel)){
            $tel=urlencode($tel);
            $tel=substr($tel, strpos($tel,'1'),11);
            if(strlen($tel)!=11) $this->send(405,config('msg.msg_405'));
        }
        return $tel;
    }
}